<?php

namespace App\Http\Controllers\Frontend;

use Laravel\Lumen\Routing\Controller as BaseController;

use App\Model\CarModel;
use App\Model\CarModelDisplacement;
use Illuminate\Http\Request;

class SeriesController extends BaseController
{

    /**
     * show all brands
     */
    public function index( Request $request, $brand ){

        $res = [
            'desc' => '查询成功！',
            'size' => '',
            'statusCode' => 1,
            'result' => []
        ];
        $brand_name = urldecode( $brand );
        $series = CarModel::where( 'brand_name', $brand_name )
//            ->orderBy('name', 'desc')
//            ->take(10)
            ->get();

        foreach ( $series as $model ) {
            $res['result'][] = [
                'C_OEM_name' => $model->factory_name,
                'c_series' => $model->name,
            ];
        }
        $res['size'] = count( $res['result'] );
        echo json_encode( $res );

    }

    /**
     * 获取指定 model 排量
     */
    public function displacement( Request $request, $factory, $series ){

        $res = [
            'desc' => '查询成功！',
            'size' => '',
            'statusCode' => 1,
            'result' => []
        ];

        $factory_name = urldecode( $factory );
        $series_name = urldecode( $series );
        $displacement = CarModelDisplacement::modelDisplacement( $factory_name, $series_name );

        foreach ( $displacement as $item ) {
            $res['result'][] = [
                'c_capacity_in_litre_l' => $item->displacement,
            ];
        }
        $res['size'] = count( $res['result'] );
        echo json_encode( $res );
    }

}
